﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fotoalbum.BLL;

namespace Fotoalbum.UI
{
    class Presenter
    {
        private readonly ILogikAdapter _logikadapter = LogikAdapter.CreateInstance();
        private IView _view;
        private Dictionary<TreeNode, BildFilter> _bildFilterDictonary = new Dictionary<TreeNode,BildFilter>();

        private IList<Bild> _bilder;

        public Presenter(IView view)
        {
            _view = view;

            _bilder = _logikadapter.AlleBilderAuslesen();

            BildFilterDictonaryFüllen();

            _view.ListViewAktualisieren(_bilder);
            _view.SetTreeNodes(TreeNodesAuslesen());
        }

        private TreeNode[] TreeNodesAuslesen()
        {
            TreeNode[] treeNodes = new TreeNode[_bildFilterDictonary.Count];

            int index = 0;
            foreach (KeyValuePair<TreeNode, BildFilter> item in _bildFilterDictonary)
            {
                treeNodes[index] = item.Key;
                index++;
            }

            return treeNodes;
        }

        private void BildFilterDictonaryFüllen()
        {
            _bildFilterDictonary.Add(new TreeNode("alle Bilder"), new BildFilter());
            _bildFilterDictonary.Add(new TreeNode("Fotoalbum"), new BildFilter() { IstNullFilter = true });
        }

        public void BildZurSammlungHinzufügen()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string pfad = openFileDialog.FileName;

                _bilder.Add(_logikadapter.BildErstellen(pfad));

                _view.ListViewAktualisieren(_bilder);
            }
        }

        public void TreeNodeSelected(TreeNode node)
        {
            BildFilter filter = _bildFilterDictonary[node];
            if (filter.IstNullFilter)
                _bilder = new List<Bild>();
            else
                _bilder = _logikadapter.BilderAuslesen(filter);

            _view.ListViewAktualisieren(_bilder);
        }
    }
}
